<template>
  <el-card class="box-card">
    <el-form
      :disabled="!isForbiddenForm"
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
    >
      <el-form-item label="一级分类">
        <!-- <el-select v-model="categoryStore.category1Id" placeholder="请选择"> -->
        <el-select v-model="category1Id" placeholder="请选择">
          <!-- :label=“item.name”//显示值
               :value=“item.id”//传给后台的值 -->
          <el-option
            :label="c1.name"
            v-for="c1 in categoryStore.category1List"
            :key="c1.id"
            :value="c1.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="category2Id" placeholder="请选择">
          <el-option
            v-for="c2 in categoryStore.category2List"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="category3Id" placeholder="请选择">
          <el-option
            v-for="c3 in categoryStore.category3List"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          />
          <el-option label="Zone two" value="beijing" />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: 'CategorySelector',
  // props:['isForbiddenForm'],
  props: {
    isForbiddenForm: {
      type: Boolean,
    },
  },
}
</script>
<script lang="ts" setup>
import { reactive, onMounted, computed } from 'vue'
import { reqGetCategory1ListData } from '@/api/product/category'
import { useCategoryListStore } from '@/stores/categoryList'
const categoryStore = useCategoryListStore()
const formInline = reactive({
  user: '',
  region: '',
})

const onSubmit = () => {
  console.log('submit!')
}

// 1. 页面挂载完成之后要发送请求获取一级导航数据
onMounted(async () => {
  // const result = await reqGetCategory1ListData()
  // console.log('一级数据请求成功', result)

  // 这个是用pinia中的方法来发送请求的
  categoryStore.getCategory1ListData()
})

// 2. 使用计算属性 精简一下category1Id
// 顺便在切换一级导航分类时，立即发送请求 获取二级导航数据
const category1Id = computed<number>({
  get() {
    return categoryStore.category1Id as number
  },
  set(val: number) {
    // categoryStore.category1Id
    // console.log('val',val)
    // 只要一修改了一级导航分类，就会执行这里面的逻辑
    // 立即调用函数，获取当前一级分类对应的二级分类数据
    categoryStore.getCategory2ListData(val)
  },
})
// 3. 使用计算数据精简一下category2Id
// 顺便二级导航一切换，立即获取三级导航分类数据
const category2Id = computed<number>({
  get() {
    return categoryStore.category2Id as number
  },
  set(val: number) {
    categoryStore.getCategory3ListData(val)
  },
})
// 4. 使用计算属性精简一下category3Id
// 顺便当三级导航发生改变的时候，要获取属性列表
const category3Id = computed<number>({
  get() {
    return categoryStore.category3Id as number
  },
  set(val: number) {
    // 将三级导航的id存储起来
    categoryStore.setCategory3Id(val)
  },
})
</script>

<style></style>
